﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SVN.Net.Models
{
    public partial class SVN : ISVNProvider
    {
        private string BuildArgs(string cmd, string uid, string pwd, bool xml)
        {
            var uid_arg = string.IsNullOrEmpty(uid) ? string.Empty : string.Format(" --username {0}", uid);
            var pwd_arg = string.IsNullOrEmpty(pwd) ? string.Empty : string.Format(" --password {0}", pwd);
            var xml_arg = " --xml";
            return string.Format("{0}{1}{2}{3}", cmd, uid_arg, pwd_arg, xml ? xml_arg : string.Empty);
        }

        private ProcessResult NormalExecute(string cmd, string uid, string pwd)
        {
            return Execute(cmd, uid, pwd, false);
        }

        private ProcessXmlResult XmlExecute(string cmd, string uid, string pwd)
        {
            return new ProcessXmlResult(Execute(cmd, uid, pwd, true));
        }

        private ProcessResult Execute(string cmd, string uid, string pwd, bool xml)
        {
            var p = new System.Diagnostics.Process();
            // Redirect the output and error information here.
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = "svn.exe";
            // Build the arguments string.
            var args = BuildArgs(cmd, uid, pwd, xml);
            p.StartInfo.Arguments = args;
            p.StartInfo.UseShellExecute = false;
            // Execute svn command.
            p.Start();
            p.StandardInput.WriteLine("t");
            // Get result.
            var result = p.StandardOutput.ReadToEnd();
            var error = p.StandardError.ReadToEnd();
            var exit = p.ExitCode;
            return new ProcessResult(exit == 0, result, error);
        }

        #region " Expired Methods "

        //private static string Command(string cmd, string uid, string pwd, bool xml)
        //{
        //    var p = new System.Diagnostics.Process();
        //    p.StartInfo.RedirectStandardOutput = true;
        //    p.StartInfo.RedirectStandardError = true;
        //    p.StartInfo.CreateNoWindow = true;
        //    p.StartInfo.FileName = "svn.exe";
        //    var args = BuildArgs(cmd, uid, pwd, xml);
        //    p.StartInfo.Arguments = args;
        //    p.StartInfo.UseShellExecute = false;
        //    p.Start();
        //    var ret = p.StandardOutput.ReadToEnd();
        //    return ret;
        //}

        //private static void NonQuery(string cmd, string uid, string pwd, bool withNoWait)
        //{
        //    string result = string.Empty;
        //    var p = new System.Diagnostics.Process();
        //    p.StartInfo.RedirectStandardOutput = true;
        //    p.StartInfo.CreateNoWindow = true;
        //    p.StartInfo.FileName = "svn.exe";
        //    var args = cmd +
        //        (string.IsNullOrEmpty(uid) ? string.Empty : string.Format(" --username {0}", uid)) +
        //        (string.IsNullOrEmpty(pwd) ? string.Empty : string.Format(" --password {0}", pwd));
        //    p.StartInfo.Arguments = args;
        //    p.StartInfo.UseShellExecute = false;
        //    p.Start();
        //    if (!withNoWait)
        //        result = p.StandardOutput.ReadToEnd();
        //}

        //private static System.Xml.XmlDocument ExecuteCommand(string cmd, string uid, string pwd)
        //{
        //    var info = Command(cmd, uid, pwd, true);
        //    var xdoc = new System.Xml.XmlDocument();
        //    xdoc.LoadXml(info);
        //    return xdoc;
        //}

        //private static void ExecuteNonQuery(string cmd, string uid, string pwd, bool withNoWait)
        //{
        //    NonQuery(cmd, uid, pwd, withNoWait);
        //}

        #endregion

    }
}
